
var swaksmack_optionsDataBoolean        = new Array();
var swaksmack_optionsDataInteger        = new Array();
var swaksmack_optionsDataString         = new Array();

var ssopts = {
	
//	var this.swaksmack_optionsDataInteger        = new Array();
//	var this.swaksmack_optionsDataString         = new Array();
	init: function() {
		var tc = SS_getPreference(SMACK_TCHL1);
		var bc = SS_getPreference(SMACK_BTCHL1);
		///org ssopts.updateSample(tc,bc);
		//alert('init page pref set ' + ssopts.isPrefSet("extensions.swaksmack.options.page_intpref"));
		if (ssopts.isPrefSet("extensions.swaksmack.options.page_intpref")) {
			var index = ssopts.getIntPref("extensions.swaksmack.options.page_intpref");
			//alert("page index " +index);
			var pageList   = document.getElementById("swaksmack-options-list");
			pageList.selectedIndex = index;
			pageList.setAttribute("src", pageList.selectedItem.value);
		}
		/*
		var cpicker = document.getElementById('tcolorpic1');
		var cpicker2 = document.getElementById('bkcolorpic1');
		
		
		var usercolor = SS_getPreference(SMACK_TCHL1);
		cpicker.color = usercolor;
		usercolor = SS_getPreference(SMACK_BTCHL1);
		cpicker2.color = usercolor;
		*/
		
	},
	getPrefService: function() {
		return Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
	},
	isPrefSet: function(preference) {
    // If the preference is set
    if(preference)
    {
    	  var preServ = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
        return preServ.prefHasUserValue(preference);
    }

    return false;
	},
getBoolPref: function(preference, userPreference) {
    // If the preference is set
    if(preference)
    {
    	//alert('ispref set ' + ssopts.isPrefSet(preference));
    	val =  Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("").getBoolPref(preference);
      //alert('bool val ' +val);
      return val;
      /*
        // If not a user preference or a user preference is set
        if(!userPreference || ssopts.isPrefSet(preference))
        {
            try
            {
                val =  Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("").getBoolPref(preference);
                alert('bool val ' +val);
                return val;
            }
            catch(exception)
            {
                // Do nothing
            }
        }
        */
    }
    

    return false;
},
// Gets an integer preference, returning 0 if the preference is not set
getIntPref: function (preference, userPreference) {
    // If the preference is set
    if(preference)
    {
    	return Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("").getIntPref(preference);
        /*
        // If not a user preference or a user preference is set
        if(!userPreference || ssopts.isPrefSet(preference))
        {
            try
            {
                return Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("").getIntPref(preference);
            }
            catch(exception)
            {
                // Do nothing
            }
        }
        */
    }

    return 0;
},
getStringPref: function(preference, userPreference) {
    // If the preference is set
    if(preference)
    {
    	return Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("").getComplexValue(preference, Components.interfaces.nsISupportsString).data.trim();
      /*
        // If not a user preference or a user preference is set
        if(!userPreference || ssopts.isPrefSet(preference))
        {
            try
            {
                return Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("").getComplexValue(preference, Components.interfaces.nsISupportsString).data.trim();
            }
            catch(exception)
            {
                // Do nothing
            }
        }
        */
    }

    return null;
},
setBoolPref: function(preference, value) {
 if(preference) {
   ssopts.getPrefService().setBoolPref(preference, value);
 }
},
setIntPref: function(preference, value) {
	if(preference) {
		try {
			ssopts.getPrefService().setIntPref(preference, value);
		}
		catch (exc) {alert('Error setting preference ' + preference)}
	}
},
setCharPref: function(preference, value) {
	if(preference) {
		try {
			ssopts.getPrefService().setCharPref(preference, value);
		}
		catch(exc) {alert('Error setting preference ' + preference)}
	}
},
clearUserPref: function(preference) {
	if (preference) {
		ssopts.getPrefService().clearUserPref(preference);
	}
},
displayBoolCheckedPref: function(preference,elementid) {
	if (preference && elementid) {
		//alert('displayBoolPref');
		var pageDocument = document.getElementById("swaksmack-options-optframe").contentDocument;
		
		if(typeof swaksmack_optionsDataBoolean[preference] == "undefined" || swaksmack_optionsDataBoolean[preference] == null) {
			pageDocument.getElementById(elementid).checked = ssopts.getBoolPref(preference, true);
		}
		else {
			pageDocument.getElementById(elementid).checked = swaksmack_optionsDataBoolean[preference];
		}
		/*
    if(typeof swaksmack_optionsDataBoolean[preference] != "undefined") {
        pageDocument.getElementById(elementid).checked = swaksmack_optionsDataBoolean[preference];
    }
    else {
    	//alert('else true');
       // pageDocument.getElementById("checkboolpref_easyurl").checked = true;
        pageDocument.getElementById(elementid).checked = ssopts.getBoolPref(preference, true);
    }
    */
	}
	else {
		alert('else displayBoolPref');
	}
},
displayIntValuePref: function(preference,elementid) {
	if (preference && elementid) {
		//alert('displayBoolPref');
		var pageDocument = document.getElementById("swaksmack-options-optframe").contentDocument;
		
		if(typeof swaksmack_optionsDataInteger[preference] == "undefined" || swaksmack_optionsDataInteger[preference] == null) {
			pageDocument.getElementById(elementid).value = ssopts.getIntPref(preference, true);
		}
		else {
			pageDocument.getElementById(elementid).value = swaksmack_optionsDataInteger[preference];
		}
		/*
    if(typeof swaksmack_optionsDataInteger[preference] != "undefined") {
        pageDocument.getElementById(elementid).value = swaksmack_optionsDataInteger[preference];
    }
    else {
    	//alert('else true');
       // pageDocument.getElementById("checkboolpref_easyurl").checked = true;
        pageDocument.getElementById(elementid).value = ssopts.getIntPref(preference, true);
    }
    */
	}
	else {
		alert('else displayBoolPref');
	}
},
displayStringValuePref: function(preference,elementid) {
	if (preference && elementid) {
		//alert('displayBoolPref');
		var pageDocument = document.getElementById("swaksmack-options-optframe").contentDocument;
		
		if(typeof swaksmack_optionsDataString[preference] == "undefined" || swaksmack_optionsDataString[preference] == null) {
			pageDocument.getElementById(elementid).value = ssopts.getStringPref(preference, true);
		}
		else {
			pageDocument.getElementById(elementid).value = swaksmack_optionsDataString[preference];
		}
		/* org
    if(typeof swaksmack_optionsDataString[preference] != "undefined") {
        pageDocument.getElementById(elementid).value = swaksmack_optionsDataString[preference];
    }
    else {
    	//alert('else true');
       // pageDocument.getElementById("checkboolpref_easyurl").checked = true;
        pageDocument.getElementById(elementid).value = ssopts.getStringPref(preference, true);
    }
    */
	}
	else {
		alert('else displayStringPref');
	}
},
displayStringColorPref: function(preference,elementid) {
	if (preference && elementid) {
		//alert('displayBoolPref');
		var pageDocument = document.getElementById("swaksmack-options-optframe").contentDocument;
		
		if(typeof swaksmack_optionsDataString[preference] == "undefined" || swaksmack_optionsDataString[preference] == null) {
		 pageDocument.getElementById(elementid).color = ssopts.getStringPref(preference, true);
		}
		else {
			pageDocument.getElementById(elementid).color = swaksmack_optionsDataString[preference];
		}
		
		/*
    if(typeof swaksmack_optionsDataString[preference] != "undefined") {
        pageDocument.getElementById(elementid).color = swaksmack_optionsDataString[preference];
    }
    else {
    	//alert('else true');
       // pageDocument.getElementById("checkboolpref_easyurl").checked = true;
        pageDocument.getElementById(elementid).color = ssopts.getStringPref(preference, true);
    }
    */
	}
	else {
		alert('else displayStringPref');
	}
},
displayStringStyleBackColorPref: function(preference,elementid) {
	if (preference && elementid) {
		//alert('displayBoolPref');
		var pageDocument = document.getElementById("swaksmack-options-optframe").contentDocument;
		
		if(typeof swaksmack_optionsDataString[preference] == "undefined" || swaksmack_optionsDataString[preference] == null) {
			pageDocument.getElementById(elementid).style.background = ssopts.getStringPref(preference, true);
		}
		else {
			pageDocument.getElementById(elementid).style.background = swaksmack_optionsDataString[preference];
		}
		
		/*
    if(typeof swaksmack_optionsDataString[preference] != "undefined") {
        pageDocument.getElementById(elementid).style.background = swaksmack_optionsDataString[preference];
    }
    else {
    	//alert('else true');
       // pageDocument.getElementById("checkboolpref_easyurl").checked = true;
        pageDocument.getElementById(elementid).style.background = ssopts.getStringPref(preference, true);
    }
    */
	}
	else {
		alert('else displayStringPref');
	}
},
displayStringStyleColorPref: function(preference,elementid) {
	if (preference && elementid) {
		//alert('displayBoolPref');
		var pageDocument = document.getElementById("swaksmack-options-optframe").contentDocument;
		
		if(typeof swaksmack_optionsDataString[preference] == "undefined" || swaksmack_optionsDataString[preference] == null) {
			pageDocument.getElementById(elementid).style.color = ssopts.getStringPref(preference, true);
		}
		else {
			pageDocument.getElementById(elementid).style.color = swaksmack_optionsDataString[preference];
		}
		
		/*
    if(typeof swaksmack_optionsDataString[preference] != "undefined") {
        pageDocument.getElementById(elementid).style.color = swaksmack_optionsDataString[preference];
    }
    else {
    	//alert('else true');
       // pageDocument.getElementById("checkboolpref_easyurl").checked = true;
        pageDocument.getElementById(elementid).style.color = ssopts.getStringPref(preference, true);
    }
    */
	}
	else {
		alert('else displayStringPref');
	}
},
	initAllOptions: function() {
		ssopts.initializeUrl();
		ssopts.initializeTinyUrl();
		ssopts.initializeAudio();
		ssopts.initializeHighlight();
		ssopts.initializeAlerts();
		ssopts.initializeIgnore();
		ssopts.initializeAdvanced();
	},
	initializeUrl: function() {
	//	alert('initUrl');
	//	var pageDocument = document.getElementById("swaksmack-options-optframe").contentDocument;

    //alert(ssopts.getBoolPref("extensions.swaksmack.easyurl_boolpref1", true));

	ssopts.displayBoolCheckedPref("extensions.swaksmack.easyurl_boolpref1", "checkboolpref_easyurl");
	ssopts.displayBoolCheckedPref("extensions.swaksmack.maxurl_boolpref1", "checkboolpref_maxurl");
	ssopts.displayIntValuePref("extensions.swaksmack.maxurllength_intpref","textintpref_maxurllength");
	ssopts.displayStringValuePref("extensions.swaksmack.truncurl_stringpref","textstringpref_truncurl");
	
	ssopts.initializeTinyUrl();
 // ssopts.displayBoolCheckedPref("extensions.swaksmack.tinyurl_enable_boolpref1", "checkboolpref_tinyurl_enable");
//	ssopts.displayIntValuePref("extensions.swaksmack.tinyurl_maxurllength_intpref","textintpref_tinyurl_maxurllength");
   
	},
	initializeTinyUrl: function() {
//		alert('initTinyUrl');
		//alert('initUrl');

  ssopts.displayBoolCheckedPref("extensions.swaksmack.tinyurl_enable_boolpref1", "checkboolpref_tinyurl_enable");
	ssopts.displayIntValuePref("extensions.swaksmack.tinyurl_maxurllength_intpref","textintpref_tinyurl_maxurllength");
	ssopts.displayBoolCheckedPref("extensions.swaksmack.tinyurl_enablepreview_boolpref", "checkboolpref_tinyurl_enablepreview");
   
	},
	initializeAudio: function() {
		ssopts.displayBoolCheckedPref("extensions.swaksmack.audio.global_enable_boolpref", "audio_global_enable");
		ssopts.displayStringValuePref("extensions.swaksmack.audio.outwitsound_stringpref","audioFile");
	},
	initializeHighlight: function() {
		ssopts.displayBoolCheckedPref("extensions.swaksmack.enablehl_boolpref1", "checkboolpref_enablehl");
		ssopts.displayBoolCheckedPref("extensions.swaksmack.ignorecasehl_boolpref1", "checkboolpref_ignorecasehl");
		ssopts.displayBoolCheckedPref("extensions.swaksmack.ignoreselfposts_boolpref1", "checkboolpref_ignoreselfposts");
		ssopts.displayBoolCheckedPref("extensions.swaksmack.enableinlrhl_boolpref1", "checkboolpref_enableinlrhl");
		ssopts.displayStringValuePref("extensions.swaksmack.keywordshl1_stringpref","textstringpref_keywordshl1");
		ssopts.displayStringValuePref("extensions.swaksmack.usernamehl1_stringpref","textstringpref_usernamehl1");
		ssopts.displayStringColorPref("extensions.swaksmack.hltextcolor1_stringpref","tcolorpic1");
		ssopts.displayStringColorPref("extensions.swaksmack.hlbkcolor1_stringpref","bkcolorpic1");
		ssopts.displayStringValuePref("extensions.swaksmack.hltextcolor1_stringpref","textstringpref_hltextcolor1");
		ssopts.displayStringValuePref("extensions.swaksmack.hlbkcolor1_stringpref","textstringpref_hlbkcolor1");
		ssopts.displayBoolCheckedPref("extensions.swaksmack.hlunderline1_boolpref1", "checkboolpref_hlunderline1");
		//ssopts.displayBoolCheckedPref("extensions.swaksmack.hlitalic1_boolpref1", "checkboolpref_ignorecasehl");
		//ssopts.displayBoolCheckedPref("extensions.swaksmack.hlbold1_boolpref1", "checkboolpref_ignoreselfposts");
		ssopts.displayBoolCheckedPref("extensions.swaksmack.usernameonlyhl_boolpref1", "checkboolpref_usernameonlyhl");
		ssopts.displayBoolCheckedPref("extensions.swaksmack.wordonlyhl_boolpref1", "checkboolpref_wordonlyhl");
		ssopts.displayBoolCheckedPref("extensions.swaksmack.postoutlinehl_boolpref1", "checkboolpref_postoutlinehl");
		ssopts.displayBoolCheckedPref("extensions.swaksmack.chatwildcard_boolpref1", "checkboolpref_chatwildcard");
		
		ssopts.displayStringStyleColorPref("extensions.swaksmack.hltextcolor1_stringpref","hlSampleText");
		ssopts.displayStringStyleBackColorPref("extensions.swaksmack.hlbkcolor1_stringpref","hlSampleText");
		
	},	
	initializeAlerts: function() {
		ssopts.displayStringValuePref("extensions.swaksmack.outwitwarn_pref1","outwitWarnMenu");
		ssopts.displayStringValuePref("extensions.swaksmack.outwitvotewarn_pref1","voteWarnMenu");
		ssopts.displayStringValuePref("extensions.swaksmack.guruguesswarn_pref1","guruWarnMenu");
    ssopts.displayBoolCheckedPref("extensions.swaksmack.outwitrandom_pref1", "checkboolpref_outwitrandom");
    ssopts.displayBoolCheckedPref("extensions.swaksmack.pmchatcheck_boolpref1", "checkboolpref_pmlrmonitorenable");
	},
	initializeIgnore: function() {
		ssopts.displayBoolCheckedPref("extensions.swaksmack.ignore.enableignoreposts_boolpref1", "checkboolpref_enableignoreposts");
		ssopts.displayStringValuePref("extensions.swaksmack.ignore.ignorelist1_stringpref","textstringpref_ignorelist1");
		ssopts.displayStringValuePref("extensions.swaksmack.ignore.ignorelist2_stringpref","textstringpref_ignorelist2");		
		ssopts.displayStringValuePref("extensions.swaksmack.ignore.ignorelistattend1_stringpref","textstringpref_ignorelistattend1");
    ssopts.displayBoolCheckedPref("extensions.swaksmack.blockwildcard_boolpref1", "checkboolpref_blockwildcard");
	},
	initializeEnhance: function() {
		ssopts.displayBoolCheckedPref("extensions.swaksmack.enhancedChatUI_boolpref","checkboolpref_enhanceenabled");
		ssopts.displayBoolCheckedPref("extensions.swaksmack.enhancedChatUIIcons_boolpref","checkboolpref_enhanceicons");
		ssopts.displayBoolCheckedPref("extensions.swaksmack.enhancedHideAvatars_boolpref","checkboolpref_enhancehideavatars");
		ssopts.displayStringValuePref("extensions.swaksmack.replyprefix_stringpref","textstringpref_replyprefix");
		ssopts.displayStringValuePref("extensions.swaksmack.replysuffix_stringpref","textstringpref_replysuffix");
		
		
	},
	initializeAppearance: function() {
		ssopts.displayBoolCheckedPref("extensions.swaksmack.appearance.displaystatusbarkw_boolpref1", "appearance_sb_kw_enable");
		ssopts.displayBoolCheckedPref("extensions.swaksmack.appearance.displaystatusbarpm_boolpref1", "appearance_sb_pm_enable");
		ssopts.displayBoolCheckedPref("extensions.swaksmack.appearance.displaystatusbarlr_boolpref1", "appearance_sb_lr_enable");
		ssopts.displayBoolCheckedPref("extensions.swaksmack.appearance.displaystatusbarsale_boolpref1", "appearance_sb_sale_enable");
	},
	initializeAdvanced: function() {
		ssopts.displayIntValuePref("extensions.swaksmack.adv.chatspeed_intpref","intpref_adv_chatspeed");
	},			
	selectPage: function(pageList) {
		//alert('select page ' + pageList.selectedItem.value);
		ssopts.cacheOptions();
		
		ssopts.setIntPref("extensions.swaksmack.options.page_intpref", pageList.selectedIndex);
		//alert("set last page " + pageList.selectedIndex);
		document.getElementById("swaksmack-options-optframe").setAttribute("src", pageList.selectedItem.value);
	},
	updateTextColor: function() {
		var iFrame  = document.getElementById("swaksmack-options-optframe");
		var pageDocument = iFrame.contentDocument;
		var cpicker = pageDocument.getElementById('tcolorpic1');
		var textColor = pageDocument.getElementById('textstringpref_hltextcolor1');
		textColor.value = cpicker.color;
		ssopts.updateSample(textColor.value, null);
	},
	updateBackgroundColor: function() {
		var iFrame  = document.getElementById("swaksmack-options-optframe");
		var pageDocument = iFrame.contentDocument;		
		var cpicker = pageDocument.getElementById('bkcolorpic1');
		var textColor = pageDocument.getElementById('textstringpref_hlbkcolor1');
		textColor.value = cpicker.color;
		ssopts.updateSample(null, textColor.value);
	},
	textboxColorChange: function(event) {
		var iFrame  = document.getElementById("swaksmack-options-optframe");
		var pageDocument = iFrame.contentDocument;		
		var textColor = event.target;
		var newcolor;
		if (textColor.id == 'textstringpref_hlbkcolor1') {
		var cpicker = pageDocument.getElementById('bkcolorpic1');
		cpicker.color = textColor.value;
		ssopts.updateSample(null, textColor.value);
		}
		else if(textColor.id == 'textstringpref_hltextcolor1') {
			var cpicker = pageDocument.getElementById('tcolorpic1');
			cpicker.color = textColor.value;
			ssopts.updateSample(textColor.value,null);
		}
		
	},
	themeColorChange: function(textColor,backgroundColor) {
	  var iFrame  = document.getElementById("swaksmack-options-optframe");
		var pageDocument = iFrame.contentDocument;
		pageDocument.getElementById('textstringpref_hltextcolor1').value = textColor;
		SS_prefs.setCharPref(SMACK_TCHL1,textColor);
		pageDocument.getElementById('textstringpref_hlbkcolor1').value = backgroundColor;
		SS_prefs.setCharPref(SMACK_BTCHL1,backgroundColor);
		
		var cpicker = pageDocument.getElementById('bkcolorpic1');
		cpicker.color = textColor;
		cpicker = pageDocument.getElementById('tcolorpic1');
		cpicker.color = backgroundColor;
		
		//ssopts.updateSample(null, textColor.value);
		ssopts.updateSample(textColor,backgroundColor);
		
	},
	updateSample: function(tc, bc) {
		var iFrame  = document.getElementById("swaksmack-options-optframe");
		var pageDocument = iFrame.contentDocument;
		var l = pageDocument.getElementById('hlSampleText');
		if (tc != null) l.style.color=tc;
		if (bc != null) l.style.background=bc;
	},
	selectAudio: function() {
		var am = swaksmack.audioManager;
		if (am == null) am = new AudioManager();
		var fc = new FileChooser('Select Audio File',am);
		fc.open();
		ssopts.initializeAudio();
	},
	playAudio: function() {
		var iFrame  = document.getElementById("swaksmack-options-optframe");
		var pageDocument = iFrame.contentDocument;
		var af = pageDocument.getElementById('audioFile').value;
		var am = swaksmack.audioManager;
		if (am == null) am = new AudioManager();
		am.playAudioAlert(af);
	},
	resetAudio: function(parent) {
		//var af = document.getElementById('audioFile').value;
		//alert('resetAudio af ' + af);
		var answer = confirm('Reset audio to default value?');
		if (answer) {
			//var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch).getBranch("");
			var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
	 		if (prefs == null) return;
			var optionitems = new Array();
	 		optionitems = optionitems.concat(SMACK_AUDIO_OPTIONITEMS);
	 		for (var i=optionitems.length-1;i>=0;i--){
			 	try {
			 		prefs.clearUserPref(optionitems[i]);
			 	} catch (Exception){}
			}
			ssopts.initializeAudio();
		}
	},
	enableGlobalAudio: function(comp) {
		
		//alert(comp.checked);
		if (comp.getAttribute("checked") == "true") {
			ssopts.setBoolPref("extensions.swaksmack.audio.global_enable_boolpref",true);
		}
		else {
			ssopts.setBoolPref("extensions.swaksmack.audio.global_enable_boolpref",false);
		}

	},
	resetIgnorePrefs: function() {
	 var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
	 //var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch).getBranch("");
	 if (prefs == null) return;
	 var answer = confirm("Reset Ignore/Block SwakSmack preferences?");
	 if (answer) {
	 	try {
	 		var optionitems = new Array();
	 		optionitems = optionitems.concat(SMACK_IGNORE_OPTIONITEMS);
	 		for (var i=optionitems.length-1;i>=0;i--){
		 	try {
		 		prefs.clearUserPref(optionitems[i]);
		 	} catch (Exception){}
		}
		
      swaksmack_optionsDataBoolean["extensions.swaksmack.blockwildcard_boolpref1"] = null;         
      swaksmack_optionsDataString["extensions.swaksmack.ignore.ignorelist1_stringpref"]     = null;
      swaksmack_optionsDataString["extensions.swaksmack.ignore.ignorelist2_stringpref"]     = null;
      swaksmack_optionsDataString["extensions.swaksmack.ignore.ignorelistattend1_stringpref"]     = null;    
		ssopts.initializeIgnore();
	 }
	 catch (Exception ) {alert('error: ' + Exception);}
	 }
	},
	resetAllPrefs: function() {
		var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");

	//org	var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch).getBranch("");
	//	alert('resetAll prefs ' + prefs);
		//var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
	  if (prefs == null) return;
		var answer = confirm("Reset All SwakSmack preferences?");
		if (answer) {
		 try {
		 	var optionitems = new Array();
		 	optionitems = optionitems.concat(SMACK_OPTIONITEMS);
		 	optionitems = optionitems.concat(SMACK_IGNORE_OPTIONITEMS);
		 	optionitems = optionitems.concat(SMACK_ADV_OPTIONITEMS);
		 	optionitems = optionitems.concat(SMACK_AUDIO_OPTIONITEMS);
		 	optionitems = optionitems.concat(SMACK_APPEARANCE_OPTIONITEMS);
		 	
		 	for (var i=optionitems.length-1;i>=0;i--){
		 		try {
		 			prefs.clearUserPref(optionitems[i]);
		 		} catch (Exception){}
			}
			swaksmack_optionsDataBoolean = [];
			swaksmack_optionsDataInteger = [];
			swaksmack_optionsDataString = [];
			
			ssopts.initializeAdvanced();
			//ssopts.cacheOptions();

		 
		// var r = prefs.deleteBranch("extensions.swaksmack."); //works, but you need to manually reset the prefs to default values
		// var r = prefs.clearUserPref(SMACK_ENABLEHL);  //works on individual prefs
		 //var r = prefs.resetBranch("extensions.swaksmack"); //not implemented

		 }
		 catch (Exception ) {alert('error: ' + Exception);}
		}
	},
	cacheOptions: function() {
		

		
	//	alert('store options entry');
    var childNodes   = null;
    var description  = null;
    var i            = 0;
    var iFrame       = document.getElementById("swaksmack-options-optframe");
    var iFrameSrc    = iFrame.getAttribute("src");
    var key          = null;
    var listCell     = null;
    var listItem     = null;
    var pageDocument = iFrame.contentDocument;
    var path         = null;
    //var stringBundle = document.getElementById("swaksmack-string-bundle");

		//alert('cacheOptions ' +iFrame);
    // If this is the colors page
    //if(iFrameSrc.indexOf("url") != -1)
    if(iFrameSrc == "chrome://swaksmack/content/options/url.xul")
    {
    	
    	  swaksmack_optionsDataBoolean["extensions.swaksmack.easyurl_boolpref1"] = pageDocument.getElementById("checkboolpref_easyurl").checked;
    	  swaksmack_optionsDataBoolean["extensions.swaksmack.maxurl_boolpref1"] = pageDocument.getElementById("checkboolpref_maxurl").checked;
        swaksmack_optionsDataInteger["extensions.swaksmack.maxurllength_intpref"]     = pageDocument.getElementById("textintpref_maxurllength").value;
        swaksmack_optionsDataString["extensions.swaksmack.truncurl_stringpref"]     = pageDocument.getElementById("textstringpref_truncurl").value;
        swaksmack_optionsDataBoolean["extensions.swaksmack.tinyurl_enable_boolpref1"] = pageDocument.getElementById("checkboolpref_tinyurl_enable").checked;
      	swaksmack_optionsDataInteger["extensions.swaksmack.tinyurl_maxurllength_intpref"]     = pageDocument.getElementById("textintpref_tinyurl_maxurllength").value;
      	swaksmack_optionsDataBoolean["extensions.swaksmack.tinyurl_enablepreview_boolpref"] = pageDocument.getElementById("checkboolpref_tinyurl_enablepreview").checked;

      	
        //swaksmack_optionsDataBoolean["extensions.swaksmack.tinyurl_enable_boolpref1"] = pageDocument.getElementById("checkboolpref_tinyurl_enable").checked;
        //swaksmack_optionsDataInteger["extensions.swaksmack.tinyurl_maxurllength_intpref"]     = pageDocument.getElementById("textintpref_tinyurl_maxurllength").value;
        //alert('url opts stored ' + swaksmack_optionsDataString["extensions.swaksmack.truncurl_stringpref"]);
    }
    else if (iFrameSrc.indexOf("audio") != -1) {
    	//alert('chathighlight opts stored');
    	swaksmack_optionsDataBoolean["extensions.swaksmack.audio.global_enable_boolpref"] = pageDocument.getElementById("audio_global_enable").checked;
    	swaksmack_optionsDataString["extensions.swaksmack.audio.outwitsound_stringpref"]     = pageDocument.getElementById("audioFile").value;
    }
    else if (iFrameSrc.indexOf("alerts") != -1) {
    	//alert('chathighlight opts stored');
    	swaksmack_optionsDataString["extensions.swaksmack.outwitwarn_pref1"]     = pageDocument.getElementById("outwitWarnMenu").value;
    	swaksmack_optionsDataString["extensions.swaksmack.outwitvotewarn_pref1"]     = pageDocument.getElementById("voteWarnMenu").value;
    	swaksmack_optionsDataString["extensions.swaksmack.guruguesswarn_pref1"]     = pageDocument.getElementById("guruWarnMenu").value;
    	swaksmack_optionsDataBoolean["extensions.swaksmack.outwitrandom_pref1"] = pageDocument.getElementById("checkboolpref_outwitrandom").checked;
    	swaksmack_optionsDataBoolean["extensions.swaksmack.pmchatcheck_boolpref1"] = pageDocument.getElementById("checkboolpref_pmlrmonitorenable").checked;
    } 
    else if (iFrameSrc.indexOf("chathighlight") != -1) {
    	//alert('chathighlight opts stored');
    	swaksmack_optionsDataString["extensions.swaksmack.keywordshl1_stringpref"]     = pageDocument.getElementById("textstringpref_keywordshl1").value;
    	swaksmack_optionsDataString["extensions.swaksmack.usernamehl1_stringpref"]     = pageDocument.getElementById("textstringpref_usernamehl1").value;
    	swaksmack_optionsDataString["extensions.swaksmack.hltextcolor1_stringpref"]     = pageDocument.getElementById("textstringpref_hltextcolor1").value;
    	swaksmack_optionsDataString["extensions.swaksmack.hlbkcolor1_stringpref"]     = pageDocument.getElementById("textstringpref_hlbkcolor1").value;
    	swaksmack_optionsDataBoolean["extensions.swaksmack.enablehl_boolpref1"] = pageDocument.getElementById("checkboolpref_enablehl").checked;
      swaksmack_optionsDataBoolean["extensions.swaksmack.ignorecasehl_boolpref1"] = pageDocument.getElementById("checkboolpref_ignorecasehl").checked;
      swaksmack_optionsDataBoolean["extensions.swaksmack.ignoreselfposts_boolpref1"] = pageDocument.getElementById("checkboolpref_ignoreselfposts").checked;
      swaksmack_optionsDataBoolean["extensions.swaksmack.enableinlrhl_boolpref1"] = pageDocument.getElementById("checkboolpref_enableinlrhl").checked;    
      swaksmack_optionsDataBoolean["extensions.swaksmack.hlunderline1_boolpref1"] = pageDocument.getElementById("checkboolpref_hlunderline1").checked;
      swaksmack_optionsDataBoolean["extensions.swaksmack.usernameonlyhl_boolpref1"] = pageDocument.getElementById("checkboolpref_usernameonlyhl").checked;
      swaksmack_optionsDataBoolean["extensions.swaksmack.wordonlyhl_boolpref1"] = pageDocument.getElementById("checkboolpref_wordonlyhl").checked;
      swaksmack_optionsDataBoolean["extensions.swaksmack.postoutlinehl_boolpref1"] = pageDocument.getElementById("checkboolpref_postoutlinehl").checked;
      swaksmack_optionsDataBoolean["extensions.swaksmack.chatwildcard_boolpref1"] = pageDocument.getElementById("checkboolpref_chatwildcard").checked;          
    }
    else if (iFrameSrc == "chrome://swaksmack/content/options/ignore.xul") {
    	swaksmack_optionsDataBoolean["extensions.swaksmack.ignore.enableignoreposts_boolpref1"] = pageDocument.getElementById("checkboolpref_enableignoreposts").checked;
      swaksmack_optionsDataBoolean["extensions.swaksmack.blockwildcard_boolpref1"] = pageDocument.getElementById("checkboolpref_blockwildcard").checked;          
      swaksmack_optionsDataString["extensions.swaksmack.ignore.ignorelist1_stringpref"]     = pageDocument.getElementById("textstringpref_ignorelist1").value;
      swaksmack_optionsDataString["extensions.swaksmack.ignore.ignorelist2_stringpref"]     = pageDocument.getElementById("textstringpref_ignorelist2").value;
      swaksmack_optionsDataString["extensions.swaksmack.ignore.ignorelistattend1_stringpref"]     = pageDocument.getElementById("textstringpref_ignorelistattend1").value;    
    }
    else if (iFrameSrc == "chrome://swaksmack/content/options/enhance.xul") {
    	swaksmack_optionsDataBoolean["extensions.swaksmack.enhancedChatUI_boolpref"] = pageDocument.getElementById("checkboolpref_enhanceenabled").checked;
    	swaksmack_optionsDataBoolean["extensions.swaksmack.enhancedChatUIIcons_boolpref"] = pageDocument.getElementById("checkboolpref_enhanceicons").checked;
    	swaksmack_optionsDataBoolean["extensions.swaksmack.enhancedHideAvatars_boolpref"] = pageDocument.getElementById("checkboolpref_enhancehideavatars").checked;
    	swaksmack_optionsDataString["extensions.swaksmack.replyprefix_stringpref"]     = pageDocument.getElementById("textstringpref_replyprefix").value;
    	swaksmack_optionsDataString["extensions.swaksmack.replysuffix_stringpref"]     = pageDocument.getElementById("textstringpref_replysuffix").value;
    	
    } 
    else if(iFrameSrc == "chrome://swaksmack/content/options/appearance.xul") {
    	swaksmack_optionsDataBoolean["extensions.swaksmack.appearance.displaystatusbarsale_boolpref1"] = pageDocument.getElementById("appearance_sb_sale_enable").checked;
    	swaksmack_optionsDataBoolean["extensions.swaksmack.appearance.displaystatusbarkw_boolpref1"] = pageDocument.getElementById("appearance_sb_kw_enable").checked;
    	swaksmack_optionsDataBoolean["extensions.swaksmack.appearance.displaystatusbarpm_boolpref1"] = pageDocument.getElementById("appearance_sb_pm_enable").checked;
    	swaksmack_optionsDataBoolean["extensions.swaksmack.appearance.displaystatusbarlr_boolpref1"] = pageDocument.getElementById("appearance_sb_lr_enable").checked;
    }    
    else if (iFrameSrc == "chrome://swaksmack/content/options/advanced.xul") {
    	swaksmack_optionsDataInteger["extensions.swaksmack.adv.chatspeed_intpref"]     = pageDocument.getElementById("intpref_adv_chatspeed").value;
    }        
    /*
    else if(iFrameSrc.indexOf("dashboard") != -1)
    {
        var updateFrequency = pageDocument.getElementById("webdeveloper.edit.update.frequency").value;

        webdeveloper_optionsDataBoolean["webdeveloper.dom.inspector.dashboard"]     = pageDocument.getElementById("webdeveloper.dom.inspector.dashboard").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.edit.wrap"]                   = pageDocument.getElementById("webdeveloper.edit.wrap").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.error.console.dashboard"]     = pageDocument.getElementById("webdeveloper.error.console.dashboard").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.style.information.dashboard"] = pageDocument.getElementById("webdeveloper.style.information.dashboard").checked;
        webdeveloper_optionsDataInteger["webdeveloper.edit.font.size"]              = pageDocument.getElementById("webdeveloper.edit.font.size").value;
        webdeveloper_optionsDataString["webdeveloper.edit.color.background"]        = pageDocument.getElementById("webdeveloper.edit.color.background").color;
        webdeveloper_optionsDataString["webdeveloper.edit.color.text"]              = pageDocument.getElementById("webdeveloper.edit.color.text").color;

        // If the update frequency is set and is a valid integer and is positive
        if(updateFrequency && parseInt(updateFrequency) == updateFrequency && updateFrequency >= 0)
        {
            webdeveloper_optionsDataInteger["webdeveloper.edit.update.frequency"] = updateFrequency;
        }
        else
        {
            webdeveloper_optionsDataInteger["webdeveloper.edit.update.frequency"] = 0;
        }
    }
    else if(iFrameSrc.indexOf("general") != -1)
    {
        webdeveloper_optionsDataBoolean["webdeveloper.context.hide"]               = pageDocument.getElementById("webdeveloper.context.hide").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.confirmation.dialogs.hide"]  = pageDocument.getElementById("webdeveloper.confirmation.dialogs.hide").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.informational.dialogs.hide"] = pageDocument.getElementById("webdeveloper.informational.dialogs.hide").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.menu.hide"]                  = pageDocument.getElementById("webdeveloper.menu.hide").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.open.tabs"]                  = pageDocument.getElementById("webdeveloper.open.tabs").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.open.tabs.background"]       = pageDocument.getElementById("webdeveloper.open.tabs.background").checked;
        webdeveloper_optionsDataString["webdeveloper.toolbar.icons"]               = pageDocument.getElementById("webdeveloper.toolbar.icons").value;
    }
    else if(iFrameSrc.indexOf("keys") != -1)
    {
        var textBox         = null;
        var textBoxes       = pageDocument.getElementById("webdeveloper-key-rows").getElementsByTagName("textbox");
        var textBoxesLength = textBoxes.length;

        // Loop through the text boxes
        for(i = 0; i < textBoxesLength; i++)
        {
            textBox = textBoxes.item(i);

            webdeveloper_optionsDataString[textBox.getAttribute("id")] = textBox.value.trim();
        }
    }
    else if(iFrameSrc.indexOf("miscellaneous") != -1)
    {
        webdeveloper_optionsDataBoolean["webdeveloper.information.tidy"]                = pageDocument.getElementById("webdeveloper.information.tidy").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.open.css.console.error"]          = pageDocument.getElementById("webdeveloper.open.css.console.error").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.open.javascript.console.warning"] = pageDocument.getElementById("webdeveloper.open.javascript.console.warning").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.open.javascript.console.error"]   = pageDocument.getElementById("webdeveloper.open.javascript.console.error").checked;
        webdeveloper_optionsDataString["webdeveloper.populate.form.fields.email"]       = pageDocument.getElementById("webdeveloper.populate.form.fields.email").value;
    }
    else if(iFrameSrc.indexOf("resize") != -1)
    {
        var height        = null;
        var resizeBox     = pageDocument.getElementById("webdeveloper-listbox");
        var resizeCount   = webdeveloper_getIntegerPreference("webdeveloper.resize.count", true);
        var viewport      = null;
        var viewportValue = false;
        var width         = null;
        var yes           = stringBundle.getString("webdeveloper_yes");

        // Loop through the previous resize options
        for(i = 1; i <= resizeCount; i++)
        {
            description = "webdeveloper.resize." + i + ".description";
            height      = "webdeveloper.resize." + i + ".height";
            width       = "webdeveloper.resize." + i + ".width";

            webdeveloper_optionsDataInteger[height]     = "";
            webdeveloper_optionsDataInteger[width]      = "";
            webdeveloper_optionsDataString[description] = "";
        }

        resizeCount = resizeBox.getRowCount();

        // Loop through the possible resize options
        for(i = 1; i <= resizeCount; i++)
        {
            description   = "webdeveloper.resize." + i + ".description";
            height        = "webdeveloper.resize." + i + ".height";
            listItem      = resizeBox.getItemAtIndex(i - 1);
            childNodes    = listItem.childNodes;
            viewport      = "webdeveloper.resize." + i + ".viewport";
            viewportValue = false;
            width         = "webdeveloper.resize." + i + ".width";

            // If the viewport label is set to yes
            if(listItem.childNodes[3].getAttribute("label") == yes)
            {
                viewportValue = true;
            }

            webdeveloper_optionsDataBoolean[viewport]   = viewportValue;
            webdeveloper_optionsDataInteger[width]      = listItem.childNodes[1].getAttribute("label");
            webdeveloper_optionsDataInteger[height]     = listItem.childNodes[2].getAttribute("label");
            webdeveloper_optionsDataString[description] = listItem.childNodes[0].getAttribute("label").trim();
        }

        webdeveloper_optionsDataInteger["webdeveloper.resize.count"] = resizeCount;
    }
    else if(iFrameSrc.indexOf("tools") != -1)
    {
        var applicationType   = stringBundle.getString("webdeveloper_application");
        var bookmarklet       = null;
        var bookmarkletType   = stringBundle.getString("webdeveloper_bookmarklet");
        var separator         = null;
        var toolBox           = pageDocument.getElementById("webdeveloper-listbox");
        var toolCount         = toolBox.getRowCount();
        var toolPreviousCount = webdeveloper_getIntegerPreference("webdeveloper.tool.count", true);
        var url               = null;

        // Loop through the previous tools
        for(i = 1; i <= toolCount || i <= toolPreviousCount; i++)
        {
            bookmarklet = "webdeveloper.tool." + i + ".bookmarklet";
            description = "webdeveloper.tool." + i + ".description";
            key         = "webdeveloper.tool." + i + ".key";
            path        = "webdeveloper.tool." + i + ".path";
            separator   = "webdeveloper.tool." + i + ".separator";
            url         = "webdeveloper.tool." + i + ".url";

            webdeveloper_optionsDataBoolean[separator]  = false;
            webdeveloper_optionsDataString[bookmarklet] = "";
            webdeveloper_optionsDataString[description] = "";
            webdeveloper_optionsDataString[key]         = "";
            webdeveloper_optionsDataString[path]        = "";
            webdeveloper_optionsDataString[url]         = "";
        }

        // Loop through the possible tools
        for(i = 1; i <= toolCount; i++)
        {
            bookmarklet = "webdeveloper.tool." + i + ".bookmarklet";
            description = "webdeveloper.tool." + i + ".description";
            listItem    = toolBox.getItemAtIndex(i - 1);
            childNodes  = listItem.childNodes;
            listCell    = listItem.childNodes[0];
            key         = "webdeveloper.tool." + i + ".key";
            path        = "webdeveloper.tool." + i + ".path";
            separator   = "webdeveloper.tool." + i + ".separator";
            url         = "webdeveloper.tool." + i + ".url";

            // If the first cell is a separator
            if(listCell.tagName == "separator")
            {
                webdeveloper_optionsDataBoolean[separator] = true;
            }
            else
            {
                webdeveloper_optionsDataString[description] = listCell.getAttribute("label").trim();
                webdeveloper_optionsDataString[key]         = listItem.childNodes[2].getAttribute("label").trim();

                // If the tool type equals application
                if(listCell.getAttribute("type") == applicationType)
                {
                    webdeveloper_optionsDataString[path] = listItem.childNodes[1].getAttribute("label").trim();
                }
                else if(listCell.getAttribute("type") == bookmarkletType)
                {
                    webdeveloper_optionsDataString[bookmarklet] = listItem.childNodes[1].getAttribute("label").trim();
                }
                else
                {
                    webdeveloper_optionsDataString[url] = listItem.childNodes[1].getAttribute("label").trim();
                }
            }
        }

        webdeveloper_optionsDataInteger["webdeveloper.tool.count"] = toolCount;
    }
    else if(iFrameSrc.indexOf("validation") != -1)
    {
        var validateLocalCSSProfile = "css21";

        // If the validate local CSS 3 radio button is selected
        if(pageDocument.getElementById("webdeveloper-validate-local-css3-radio").selected)
        {
            validateLocalCSSProfile = "css3";
        }

        webdeveloper_optionsDataBoolean["webdeveloper.validate.local.accessibility.section508"]    = pageDocument.getElementById("webdeveloper.validate.local.accessibility.section508").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.validate.local.accessibility.wai.priority1"] = pageDocument.getElementById("webdeveloper.validate.local.accessibility.wai.priority1").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.validate.local.accessibility.wai.priority2"] = pageDocument.getElementById("webdeveloper.validate.local.accessibility.wai.priority2").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.validate.local.accessibility.wai.priority3"] = pageDocument.getElementById("webdeveloper.validate.local.accessibility.wai.priority3").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.validate.local.html.show.outline"]           = pageDocument.getElementById("webdeveloper.validate.local.html.show.outline").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.validate.local.html.show.parse.tree"]        = pageDocument.getElementById("webdeveloper.validate.local.html.show.parse.tree").checked;
        webdeveloper_optionsDataBoolean["webdeveloper.validate.local.html.show.source"]            = pageDocument.getElementById("webdeveloper.validate.local.html.show.source").checked;
        webdeveloper_optionsDataString["webdeveloper.validate.local.css.profile"]                  = validateLocalCSSProfile;
    }
    else if(iFrameSrc.indexOf("view_source") != -1)
    {
        var viewSourceWithBox           = pageDocument.getElementById("webdeveloper-listbox");
        var viewSourceWithCount         = viewSourceWithBox.getRowCount();
        var viewSourceWithPreviousCount = webdeveloper_getIntegerPreference("webdeveloper.view.source.with.count", true);

        // Loop through the previous view source with options
        for(i = 1; i <= viewSourceWithCount || i <= viewSourceWithPreviousCount; i++)
        {
            description = "webdeveloper.view.source.with." + i + ".description";
            key         = "webdeveloper.view.source.with." + i + ".key";
            path        = "webdeveloper.view.source.with." + i + ".path";

            webdeveloper_optionsDataString[description] = "";
            webdeveloper_optionsDataString[key]         = "";
            webdeveloper_optionsDataString[path]        = "";
        }

        // Loop through the possible view source with options
        for(i = 1; i <= viewSourceWithCount; i++)
        {
            description = "webdeveloper.view.source.with." + i + ".description";
            listItem    = viewSourceWithBox.getItemAtIndex(i - 1);
            childNodes  = listItem.childNodes;
            key         = "webdeveloper.view.source.with." + i + ".key";
            path        = "webdeveloper.view.source.with." + i + ".path";

            webdeveloper_optionsDataString[description] = listItem.childNodes[0].getAttribute("label").trim();
            webdeveloper_optionsDataString[key]         = listItem.childNodes[2].getAttribute("label").trim();
            webdeveloper_optionsDataString[path]        = listItem.childNodes[1].getAttribute("label").trim();
        }

        webdeveloper_optionsDataBoolean["webdeveloper.view.source.tab"]        = pageDocument.getElementById("webdeveloper.view.source.tab").checked;
        webdeveloper_optionsDataInteger["webdeveloper.view.source.with.count"] = viewSourceWithCount;
    }
    */
},
saveOptions: function() {
    var parentWindow = null;
    var option       = null;
    var optionValue  = null;

    // Make sure current page is stored
    ssopts.cacheOptions();
  //  alert('saveOptions');
    // Loop through the boolean options
    for(option in swaksmack_optionsDataBoolean)
    {
        ssopts.setBoolPref(option, swaksmack_optionsDataBoolean[option]);
    }

    // Loop through the integer options
    for(option in swaksmack_optionsDataInteger)
    {
        optionValue = swaksmack_optionsDataInteger[option];

        // If the option value is set
        if(optionValue)
        {
            ssopts.setIntPref(option, optionValue);
        }
        else if(ssopts.isPrefSet(option))
        {
            ssopts.clearUserPref(option);
        }
    }

    // Loop through the string options
    for(option in swaksmack_optionsDataString)
    {
        optionValue = swaksmack_optionsDataString[option];

        // If the option value is set or the preference currently has a value
        if(optionValue || ssopts.isPrefSet(option) || optionValue == '')
        {
            ssopts.setCharPref(option, optionValue);
        }
    }

    //alert('save options done');
    //SS_onOptionsUpdate();

   ///??? webdeveloper_changeOptions();
   // webdeveloper_configureKeyboardShortcuts(false);
   
   
}
} //end ssopts
